package com.ecoolex.ying.space.api.graphql;

import java.util.List;
import java.util.stream.Collectors;

import org.apache.dubbo.config.annotation.Reference;
import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;

import com.alibaba.fastjson.JSON;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.service.storage.StorageInfo;
import com.ecoolex.framework.modularize.api.service.storage.StorageService;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.ecoolex.ying.space.entity.EstateProjectActivityExternalGoods;
import com.ecoolex.ying.space.entity.EstateProjectActivityExternalGoodsCategory;
import com.ecoolex.ying.space.ws.EstateProjectActivityExternalGoodsService;

@GraphQL2Module(type = GraphQL2Module.Type.User,description = "地产项目外部热门商品")
public class EstateProjectActivityExternalGoodsGraphql {

	@Reference
	private EstateProjectActivityExternalGoodsService service;
	
	@Autowired
    private StorageService storageService;
	
	@TypeField(type = EstateProjectActivityExternalGoods.class, field = "image", description = "地产项目外部热门商品封面图片")
    public StorageInfo image(@Source EstateProjectActivityExternalGoods source) {
        return storageService.get(source.getImage());
    }
	
	@TypeField(type = EstateProjectActivityExternalGoods.class, field = "images", description = "地产项目外部热门商品轮播图片")
	public List<StorageInfo> images(@Source EstateProjectActivityExternalGoods source){
		if(Check.notNullOrTrimEmpty(source.getImages())) {
			return JSON.parseArray(source.getImages(), String.class).stream()
				.map(storageService::get)
				.filter(Check::notNull)
				.collect(Collectors.toList());
		}
		return Lists.newArrayList();
	}
	
	@QueryField(field = "estateProjectActivityExternalGoods" , description = "地产项目外部热门商品")
	public EstateProjectActivityExternalGoods get(@NonNull Integer externalGoodsId) {
		return service.get(externalGoodsId);
	}
	
	@QueryField(field = "estateProjectActivityExternalGoodsList" , description = "地产项目外部热门商品列表")
	public Pagination<EstateProjectActivityExternalGoods> query(@NonNull Integer projectId,Integer externalGoodsCategoryId,@NonNull Page page) {
		return service.queryForH5(projectId,externalGoodsCategoryId,page);
	}
	
	@QueryField(field = "estateProjectActivityExternalGoodsCategory" , description = "地产项目外部热门商品分类")
	public EstateProjectActivityExternalGoodsCategory getCategory(@NonNull Integer externalGoodsCategoryId) {
		return service.getCategory(externalGoodsCategoryId);
	}
	
	@QueryField(field = "estateProjectActivityExternalGoodsCategoryList" , description = "地产项目外部热门商品分类列表")
	public Pagination<EstateProjectActivityExternalGoodsCategory> query(@NonNull Integer projectId,String name,@NonNull Page page){
		return service.queryCategoryForH5(projectId, page);
	}
}
